﻿using AutoMapper;
using MediatR;
using S.Common.ApiResult;
using S.Weaning.Domain;
using S.Weaning.Domain.Dto;
using S.Weaning.Infrastructure.Interface;
using S.Weaning.Read.ApI.Application.Command;
using static S.Weaning.Read.ApI.Application.Command.LambAblactationCommand;
using static S.Weaning.Read.ApI.Application.Command.MaternalWeaningCommand;

namespace S.Weaning.Read.ApI.Application.CommandHandlers
{
    public class LambAblactationCommandHandler : IRequestHandler<ShowLambAblactationCommand, ApiResult<List<LambAblactationDto>>>,IRequestHandler<IndexLambAblactationCommand,List<LambAblactationDto>>, IRequestHandler<LambAblactationByIdCommand, LambAblactationModel>
    {
        private readonly ILambAblactationRepository lambAblactationRepository;
        private readonly IMapper mapper;

        public LambAblactationCommandHandler(ILambAblactationRepository lambAblactationRepository,IMapper mapper)
        {
            this.lambAblactationRepository = lambAblactationRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 羔羊断奶
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<LambAblactationDto>>> Handle(ShowLambAblactationCommand request, CancellationToken cancellationToken)
        {
            var resultList = lambAblactationRepository.GetAllLambAblactations(request.Ddate, request.Ear, request.Dayage, request.Weightmin,request.Weightgmax,request.Maternal);
            var dto = mapper.Map<List<LambAblactationDto>>(resultList).OrderByDescending(t => t.WeaningDate).ToList();
            ApiResult<List<LambAblactationDto>> result = new ApiResult<List<LambAblactationDto>>();
            result.Data = dto;
            result.totalCount = dto.Count;
            result.pageCount = (int)Math.Ceiling(result.totalCount * 1.0 / request.Size);
            result.Data = dto.Skip((request.Page - 1) * request.Size).Take(request.Size).ToList();
            return result;
        }
        /// <summary>
        /// 羔羊信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<LambAblactationDto>> Handle(IndexLambAblactationCommand request, CancellationToken cancellationToken)
        {
            var list= lambAblactationRepository.GetAll().ToList();
            var dto = mapper.Map<List<LambAblactationDto>>(list);
            return dto;

        }
        /// <summary>
        /// 羔羊断奶反填（修改）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<LambAblactationModel> Handle(LambAblactationByIdCommand request, CancellationToken cancellationToken)
        {
           var a= lambAblactationRepository.GetAll().FirstOrDefault(t=>t.LambEarNumberId==request.id);
            return a;
        }
    }
}
